﻿/**
 * Created in 2012-2-23.
 */
package kit4j.generator.keyed;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import kit4j.generator.keyless.IKeylessGenerator;
import kit4j.generator.keyless.RandomAlnumStringKeylessGenerator;
import kit4j.generator.keyless.RandomByteKeylessGenerator;
import kit4j.generator.keyless.RandomColorKeylessGenerator;
import kit4j.generator.keyless.RandomIntegerKeylessGenerator;

/**
 * 指定类型的随机对象生成器。
 * 
 * @author MiniKnife
 * 
 */
public class RandomObjectKeyedGenerator implements
		IKeyedGenerator<Class<?>, Object> {

	static Map<Class<?>, IKeylessGenerator<?>> generatorMap = createGeneratorMap();
	
	private static Map<Class<?>, IKeylessGenerator<?>> createGeneratorMap() {
		Map<Class<?>, IKeylessGenerator<?>> map = new HashMap<Class<?>, IKeylessGenerator<?>>();
		
		map.put(Integer.TYPE, new RandomIntegerKeylessGenerator());
		map.put(Integer.class, new RandomIntegerKeylessGenerator());
		map.put(Byte.class, new RandomByteKeylessGenerator());
		map.put(Byte.TYPE, new RandomByteKeylessGenerator());
		map.put(String.class, new RandomAlnumStringKeylessGenerator());
		map.put(Color.class, new RandomColorKeylessGenerator());
		
		return map;
	}

	/**
	 * @see kit4j.generator.keyed.IKeyedGenerator#next(java.lang.Object)
	 */
	@Override
	public Object next(Class<?> clazz) {
		if(generatorMap.containsKey(clazz)) {
			return generatorMap.get(clazz).next();
		} else {
			return createInstance(clazz);
		}
	}
	
	protected Object createInstance(Class<?> clazz) {
		try {
			return clazz.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
